package cn.panda.edu.student;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import cn.panda.edu.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.panda.edu.model.Student;
import cn.panda.edu.model.Transaction;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.ResultDataUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/api/student/transaction")
@Validated
public class StudentTransactionController {
	@Autowired
	private TransactionService transactionService;
	@ApiOperation(value="异动申请列表" , notes="学生申请请假,休学等操作")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer",paramType="form"),
        @ApiImplicitParam(name = "studentId", value = "学号", required = true, dataType = "Integer",paramType="form")
	})
	@RequestMapping(value="/queryTransaction",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> queryTransaction(HttpSession session , @RequestParam(defaultValue="1",name="pageIndex") Integer pageIndex){
		Student student = (Student) session.getAttribute(Constant.LOGIN_USER);
		if(student==null){
			student=new Student();
			student.setStudentId(1);
		}
		PageInfo<Transaction> pageInfo = transactionService.listAll(pageIndex, Constant.PAGE_SIZE, student.getStudentId());
		return ResultDataUtil.resData(200, "", pageInfo);	
	}
	
	@RequestMapping(value="/validation",method=RequestMethod.POST)
	@ResponseBody
	public void checkTransaction(@Valid Transaction transaction,BindingResult result){
		if(result.hasErrors()) {
			for(ObjectError error:result.getAllErrors()) {
				System.out.println(error.getDefaultMessage());
			}
		}
	}
	
	@ApiOperation(value="填写异动申请表" , notes="学生填写异动申请如：请假、休学等操作")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "studentId", value = "学号", required = true, dataType = "Transaction",paramType="form")
	})
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public Object saveTransaction(Transaction transaction){
		int status=200;
		String message=null;
		transaction.setTransactionDelete(1);
		int i = transactionService.insertTransaction(transaction);
		if(i > 0) {
			status=200;
			message="添加成功";
		} else {
			status=500;
			message="添加失败";
		}
		return ResultDataUtil.resData(status, message, null);
	}
}
